using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
	/// <summary> 
	///Author: nnamthach@gmail.com 
	/// <summary>
    public class OpGioDataAccess : Connection
    {
		#region Local Variable
        public OpGioDataAccess(string type, string connectString, int timeout = 0) : base(type, connectString, timeout) { }
        public OpGioDataAccess(Connection connection) : base(connection) { }
		#endregion Local Variable
		
		#region Method        
		public DataTable GetShortData(String XuongId, List<DTO.FilterParameter> filters, ref string sErr) {
            string sql = @"SELECT LineId  
            FROM  OpGio
            WHERE  XuongId = @XuongId";
			
			bool hasfilters = BeginFilterParameter(ref sql, filters);

            InitConnect();
            InitCommand(sql);
            AddParameter(OpGioInfo.Field.XuongId, XuongId);
            if (hasfilters) AddFilterParameter(filters);
			
            DataTable list = new DataTable();
            try { list = executeSelect(); }
            catch (Exception ex) { sErr = string.Format("{0} sql='{1}'", ex.Message, sql); }

            return list;
        }
		
		public DataTable GetByFilterToDataTable(String XuongId, List<DTO.FilterParameter> filters, ref string sErr, int indexPage = 0, int itemPerPage = 0) {
            string sql = @"SELECT  XuongId, LineId, BatDau, GioLam1, GioLam2, GioLam3, GioLam4, GioLam5, GioLam6, GioLam7, GioLam8, GioLam9, GioLam10, GioLam11, GioLam12, GioLam13, GioLam14, GioLam15, GioLam16, GioLam17, GioLam18, GioLam19, GioLam20, GioNghi1, GioNghi2, GioNghi3, GioNghi4, GioNghi5, GioNghi6, GioNghi7, GioNghi8, GioNghi9, GioNghi10, GioNghi11, GioNghi12, GioNghi13, GioNghi14, GioNghi15, GioNghi16, GioNghi17, GioNghi18, GioNghi19, GioNghi20,SoGiaiDoan
            FROM  OpGio
            WHERE  XuongId = @XuongId";

            bool hasfilters = BeginFilterParameter(ref sql, filters);
			
			sql += @" ORDER BY  XuongId, LineId";
			
            if (itemPerPage != 0) {
                if (DbType == "S") sql += @" OFFSET @indexPage ROWS FETCH NEXT @itemPerPage ROWS ONLY";
                else sql += @" LIMIT @indexPage ,@itemPerPage ";
            }
            
            InitConnect();
            InitCommand(sql);
            AddParameter(OpGioInfo.Field.XuongId, XuongId);
            if (itemPerPage != 0) {
                AddParameter("indexPage", indexPage);
                AddParameter("itemPerPage", itemPerPage);
            }

			if (hasfilters) AddFilterParameter(filters);
			
            DataTable list = new DataTable();
            try { list = executeSelect(); }
			catch (Exception ex) { sErr = string.Format("{0} sql='{1}'", ex.Message, sql); }

            return list;
        }
		
		
		public OpGioInfo[] GetByFilter(String XuongId, List<DTO.FilterParameter> filters, ref string sErr, int indexPage = 0, int itemPerPage = 0) {
			DataTable list = GetByFilterToDataTable(XuongId, filters, ref sErr, indexPage, itemPerPage);

			if (sErr != "") return null;
			
            OpGioInfo[] res = null;            
			
			try
			{
				res = new OpGioInfo[list.Rows.Count];
				for (int i = 0; i < list.Rows.Count; i++)
					res[i] = new OpGioInfo(list.Rows[i]);
			}
			catch (Exception ex) { sErr = ex.Message; }
            
			if (sErr != "") return null;
            return res;
        }
		
		public int GetCountRecord(String XuongId, List<DTO.FilterParameter> filters, ref string sErr) {
            string sql = @"SELECT COUNT(*)
                FROM  OpGio
                WHERE  XuongId = @XuongId";
				
			bool hasfilters = BeginFilterParameter(ref sql, filters);
			
			int ret = -1;
            openConnection();
            InitCommand(sql);
            AddParameter(OpGioInfo.Field.XuongId, XuongId);
          
			if (hasfilters) AddFilterParameter(filters);
		  
            try
            {
                object tmp = executeScalar();

                if(tmp != null && tmp != DBNull.Value) ret = Convert.ToInt32(tmp);
				else ret = 0;
            }
            catch (Exception ex) { sErr = string.Format("{0} sql='{1}'", ex.Message, sql); }
            finally { closeConnection(); }
			
            return ret;			
        }
		
		/// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public int Add(OpGioInfo objEntr, ref string sErr) {
            string sqlFields = @" INSERT INTO OpGio(XuongId,LineId,";
            string sqlValues = @" VALUES(@XuongId,@LineId,";

            if (objEntr.BatDau != null) { sqlFields += " BatDau,"; sqlValues += " @BatDau,"; }
            if (objEntr.GioLam1 != null) { sqlFields += " GioLam1,"; sqlValues += " @GioLam1,"; }
            if (objEntr.GioLam2 != null) { sqlFields += " GioLam2,"; sqlValues += " @GioLam2,"; }
            if (objEntr.GioLam3 != null) { sqlFields += " GioLam3,"; sqlValues += " @GioLam3,"; }
            if (objEntr.GioLam4 != null) { sqlFields += " GioLam4,"; sqlValues += " @GioLam4,"; }
            if (objEntr.GioLam5 != null) { sqlFields += " GioLam5,"; sqlValues += " @GioLam5,"; }
            if (objEntr.GioLam6 != null) { sqlFields += " GioLam6,"; sqlValues += " @GioLam6,"; }
            if (objEntr.GioLam7 != null) { sqlFields += " GioLam7,"; sqlValues += " @GioLam7,"; }
            if (objEntr.GioLam8 != null) { sqlFields += " GioLam8,"; sqlValues += " @GioLam8,"; }
            if (objEntr.GioLam9 != null) { sqlFields += " GioLam9,"; sqlValues += " @GioLam9,"; }
            if (objEntr.GioLam10 != null) { sqlFields += " GioLam10,"; sqlValues += " @GioLam10,"; }
            if (objEntr.GioLam11 != null) { sqlFields += " GioLam11,"; sqlValues += " @GioLam11,"; }
            if (objEntr.GioLam12 != null) { sqlFields += " GioLam12,"; sqlValues += " @GioLam12,"; }
            if (objEntr.GioLam13 != null) { sqlFields += " GioLam13,"; sqlValues += " @GioLam13,"; }
            if (objEntr.GioLam14 != null) { sqlFields += " GioLam14,"; sqlValues += " @GioLam14,"; }
            if (objEntr.GioLam15 != null) { sqlFields += " GioLam15,"; sqlValues += " @GioLam15,"; }
            if (objEntr.GioLam16 != null) { sqlFields += " GioLam16,"; sqlValues += " @GioLam16,"; }
            if (objEntr.GioLam17 != null) { sqlFields += " GioLam17,"; sqlValues += " @GioLam17,"; }
            if (objEntr.GioLam18 != null) { sqlFields += " GioLam18,"; sqlValues += " @GioLam18,"; }
            if (objEntr.GioLam19 != null) { sqlFields += " GioLam19,"; sqlValues += " @GioLam19,"; }
            if (objEntr.GioLam20 != null) { sqlFields += " GioLam20,"; sqlValues += " @GioLam20,"; }
            if (objEntr.GioNghi1 != null) { sqlFields += " GioNghi1,"; sqlValues += " @GioNghi1,"; }
            if (objEntr.GioNghi2 != null) { sqlFields += " GioNghi2,"; sqlValues += " @GioNghi2,"; }
            if (objEntr.GioNghi3 != null) { sqlFields += " GioNghi3,"; sqlValues += " @GioNghi3,"; }
            if (objEntr.GioNghi4 != null) { sqlFields += " GioNghi4,"; sqlValues += " @GioNghi4,"; }
            if (objEntr.GioNghi5 != null) { sqlFields += " GioNghi5,"; sqlValues += " @GioNghi5,"; }
            if (objEntr.GioNghi6 != null) { sqlFields += " GioNghi6,"; sqlValues += " @GioNghi6,"; }
            if (objEntr.GioNghi7 != null) { sqlFields += " GioNghi7,"; sqlValues += " @GioNghi7,"; }
            if (objEntr.GioNghi8 != null) { sqlFields += " GioNghi8,"; sqlValues += " @GioNghi8,"; }
            if (objEntr.GioNghi9 != null) { sqlFields += " GioNghi9,"; sqlValues += " @GioNghi9,"; }
            if (objEntr.GioNghi10 != null) { sqlFields += " GioNghi10,"; sqlValues += " @GioNghi10,"; }
            if (objEntr.GioNghi11 != null) { sqlFields += " GioNghi11,"; sqlValues += " @GioNghi11,"; }
            if (objEntr.GioNghi12 != null) { sqlFields += " GioNghi12,"; sqlValues += " @GioNghi12,"; }
            if (objEntr.GioNghi13 != null) { sqlFields += " GioNghi13,"; sqlValues += " @GioNghi13,"; }
            if (objEntr.GioNghi14 != null) { sqlFields += " GioNghi14,"; sqlValues += " @GioNghi14,"; }
            if (objEntr.GioNghi15 != null) { sqlFields += " GioNghi15,"; sqlValues += " @GioNghi15,"; }
            if (objEntr.GioNghi16 != null) { sqlFields += " GioNghi16,"; sqlValues += " @GioNghi16,"; }
            if (objEntr.GioNghi17 != null) { sqlFields += " GioNghi17,"; sqlValues += " @GioNghi17,"; }
            if (objEntr.GioNghi18 != null) { sqlFields += " GioNghi18,"; sqlValues += " @GioNghi18,"; }
            if (objEntr.GioNghi19 != null) { sqlFields += " GioNghi19,"; sqlValues += " @GioNghi19,"; }
            if (objEntr.GioNghi20 != null) { sqlFields += " GioNghi20,"; sqlValues += " @GioNghi20,"; }
            if (objEntr.SoGiaiDoan != null) { sqlFields += " SoGiaiDoan,"; sqlValues += " @SoGiaiDoan,"; }
            
/* Can not combine this 			if (objEntr.<%CreateColumn(Name)%> != null) { sqlFields += " <%CreateColumn(FieldName)%>,"; sqlValues += " @<%CreateColumn(FieldName)%>,"; }*/
/* Can not combine this 			if (objEntr.<%UpdateColumn(Name)%> != null) { sqlFields += " <%UpdateColumn(FieldName)%>,"; sqlValues += " @<%UpdateColumn(FieldName)%>,"; }*/

            sqlFields = sqlFields.Substring(0, sqlFields.Length - 1) + ")";
            sqlValues = sqlValues.Substring(0, sqlValues.Length - 1) + ")";

            int ret = -1;
			string sql = sqlFields + sqlValues;
            openConnection();
            InitCommand(sql);
            AddParameter(OpGioInfo.Field.XuongId, objEntr.XuongId);
            AddParameter(OpGioInfo.Field.LineId, objEntr.LineId);

            if (objEntr.BatDau != null) AddParameter(OpGioInfo.Field.BatDau, objEntr.BatDau);         
            if (objEntr.GioLam1 != null) AddParameter(OpGioInfo.Field.GioLam1, objEntr.GioLam1);         
            if (objEntr.GioLam2 != null) AddParameter(OpGioInfo.Field.GioLam2, objEntr.GioLam2);         
            if (objEntr.GioLam3 != null) AddParameter(OpGioInfo.Field.GioLam3, objEntr.GioLam3);         
            if (objEntr.GioLam4 != null) AddParameter(OpGioInfo.Field.GioLam4, objEntr.GioLam4);         
            if (objEntr.GioLam5 != null) AddParameter(OpGioInfo.Field.GioLam5, objEntr.GioLam5);         
            if (objEntr.GioLam6 != null) AddParameter(OpGioInfo.Field.GioLam6, objEntr.GioLam6);         
            if (objEntr.GioLam7 != null) AddParameter(OpGioInfo.Field.GioLam7, objEntr.GioLam7);         
            if (objEntr.GioLam8 != null) AddParameter(OpGioInfo.Field.GioLam8, objEntr.GioLam8);         
            if (objEntr.GioLam9 != null) AddParameter(OpGioInfo.Field.GioLam9, objEntr.GioLam9);         
            if (objEntr.GioLam10 != null) AddParameter(OpGioInfo.Field.GioLam10, objEntr.GioLam10);         
            if (objEntr.GioLam11 != null) AddParameter(OpGioInfo.Field.GioLam11, objEntr.GioLam11);         
            if (objEntr.GioLam12 != null) AddParameter(OpGioInfo.Field.GioLam12, objEntr.GioLam12);         
            if (objEntr.GioLam13 != null) AddParameter(OpGioInfo.Field.GioLam13, objEntr.GioLam13);         
            if (objEntr.GioLam14 != null) AddParameter(OpGioInfo.Field.GioLam14, objEntr.GioLam14);         
            if (objEntr.GioLam15 != null) AddParameter(OpGioInfo.Field.GioLam15, objEntr.GioLam15);         
            if (objEntr.GioLam16 != null) AddParameter(OpGioInfo.Field.GioLam16, objEntr.GioLam16);         
            if (objEntr.GioLam17 != null) AddParameter(OpGioInfo.Field.GioLam17, objEntr.GioLam17);         
            if (objEntr.GioLam18 != null) AddParameter(OpGioInfo.Field.GioLam18, objEntr.GioLam18);         
            if (objEntr.GioLam19 != null) AddParameter(OpGioInfo.Field.GioLam19, objEntr.GioLam19);         
            if (objEntr.GioLam20 != null) AddParameter(OpGioInfo.Field.GioLam20, objEntr.GioLam20);         
            if (objEntr.GioNghi1 != null) AddParameter(OpGioInfo.Field.GioNghi1, objEntr.GioNghi1);         
            if (objEntr.GioNghi2 != null) AddParameter(OpGioInfo.Field.GioNghi2, objEntr.GioNghi2);         
            if (objEntr.GioNghi3 != null) AddParameter(OpGioInfo.Field.GioNghi3, objEntr.GioNghi3);         
            if (objEntr.GioNghi4 != null) AddParameter(OpGioInfo.Field.GioNghi4, objEntr.GioNghi4);         
            if (objEntr.GioNghi5 != null) AddParameter(OpGioInfo.Field.GioNghi5, objEntr.GioNghi5);         
            if (objEntr.GioNghi6 != null) AddParameter(OpGioInfo.Field.GioNghi6, objEntr.GioNghi6);         
            if (objEntr.GioNghi7 != null) AddParameter(OpGioInfo.Field.GioNghi7, objEntr.GioNghi7);         
            if (objEntr.GioNghi8 != null) AddParameter(OpGioInfo.Field.GioNghi8, objEntr.GioNghi8);         
            if (objEntr.GioNghi9 != null) AddParameter(OpGioInfo.Field.GioNghi9, objEntr.GioNghi9);         
            if (objEntr.GioNghi10 != null) AddParameter(OpGioInfo.Field.GioNghi10, objEntr.GioNghi10);         
            if (objEntr.GioNghi11 != null) AddParameter(OpGioInfo.Field.GioNghi11, objEntr.GioNghi11);         
            if (objEntr.GioNghi12 != null) AddParameter(OpGioInfo.Field.GioNghi12, objEntr.GioNghi12);         
            if (objEntr.GioNghi13 != null) AddParameter(OpGioInfo.Field.GioNghi13, objEntr.GioNghi13);         
            if (objEntr.GioNghi14 != null) AddParameter(OpGioInfo.Field.GioNghi14, objEntr.GioNghi14);         
            if (objEntr.GioNghi15 != null) AddParameter(OpGioInfo.Field.GioNghi15, objEntr.GioNghi15);         
            if (objEntr.GioNghi16 != null) AddParameter(OpGioInfo.Field.GioNghi16, objEntr.GioNghi16);         
            if (objEntr.GioNghi17 != null) AddParameter(OpGioInfo.Field.GioNghi17, objEntr.GioNghi17);         
            if (objEntr.GioNghi18 != null) AddParameter(OpGioInfo.Field.GioNghi18, objEntr.GioNghi18);         
            if (objEntr.GioNghi19 != null) AddParameter(OpGioInfo.Field.GioNghi19, objEntr.GioNghi19);         
            if (objEntr.GioNghi20 != null) AddParameter(OpGioInfo.Field.GioNghi20, objEntr.GioNghi20);
            if (objEntr.SoGiaiDoan != null) AddParameter(OpGioInfo.Field.SoGiaiDoan, objEntr.SoGiaiDoan);         
            
/* Can not combine this 			if (objEntr.<%CreateColumn(Name)%> != null) AddParameter(OpGioInfo.Field.<%CreateColumn(Name)%>, objEntr.<%CreateColumn(Name)%>);*/
/* Can not combine this 			if (objEntr.<%UpdateColumn(Name)%> != null) AddParameter(OpGioInfo.Field.<%UpdateColumn(Name)%>, objEntr.<%UpdateColumn(Name)%>);*/
          
            try
            {
                object tmp = executeScalar();
                if(tmp != null && tmp != DBNull.Value) ret = Convert.ToInt32(tmp);
				else ret = 0;
            }
            catch (Exception ex) { sErr = string.Format("{0} sql='{1}'", ex.Message, sql); }
            finally { closeConnection(); }

            return ret;
        }

        public string Update(OpGioInfo objEntr) {
            string sql = @"UPDATE OpGio SET ";
			
            if (objEntr.BatDau != null) { sql += "BatDau = @BatDau,"; }
            if (objEntr.GioLam1 != null) { sql += "GioLam1 = @GioLam1,"; }
            if (objEntr.GioLam2 != null) { sql += "GioLam2 = @GioLam2,"; }
            if (objEntr.GioLam3 != null) { sql += "GioLam3 = @GioLam3,"; }
            if (objEntr.GioLam4 != null) { sql += "GioLam4 = @GioLam4,"; }
            if (objEntr.GioLam5 != null) { sql += "GioLam5 = @GioLam5,"; }
            if (objEntr.GioLam6 != null) { sql += "GioLam6 = @GioLam6,"; }
            if (objEntr.GioLam7 != null) { sql += "GioLam7 = @GioLam7,"; }
            if (objEntr.GioLam8 != null) { sql += "GioLam8 = @GioLam8,"; }
            if (objEntr.GioLam9 != null) { sql += "GioLam9 = @GioLam9,"; }
            if (objEntr.GioLam10 != null) { sql += "GioLam10 = @GioLam10,"; }
            if (objEntr.GioLam11 != null) { sql += "GioLam11 = @GioLam11,"; }
            if (objEntr.GioLam12 != null) { sql += "GioLam12 = @GioLam12,"; }
            if (objEntr.GioLam13 != null) { sql += "GioLam13 = @GioLam13,"; }
            if (objEntr.GioLam14 != null) { sql += "GioLam14 = @GioLam14,"; }
            if (objEntr.GioLam15 != null) { sql += "GioLam15 = @GioLam15,"; }
            if (objEntr.GioLam16 != null) { sql += "GioLam16 = @GioLam16,"; }
            if (objEntr.GioLam17 != null) { sql += "GioLam17 = @GioLam17,"; }
            if (objEntr.GioLam18 != null) { sql += "GioLam18 = @GioLam18,"; }
            if (objEntr.GioLam19 != null) { sql += "GioLam19 = @GioLam19,"; }
            if (objEntr.GioLam20 != null) { sql += "GioLam20 = @GioLam20,"; }
            if (objEntr.GioNghi1 != null) { sql += "GioNghi1 = @GioNghi1,"; }
            if (objEntr.GioNghi2 != null) { sql += "GioNghi2 = @GioNghi2,"; }
            if (objEntr.GioNghi3 != null) { sql += "GioNghi3 = @GioNghi3,"; }
            if (objEntr.GioNghi4 != null) { sql += "GioNghi4 = @GioNghi4,"; }
            if (objEntr.GioNghi5 != null) { sql += "GioNghi5 = @GioNghi5,"; }
            if (objEntr.GioNghi6 != null) { sql += "GioNghi6 = @GioNghi6,"; }
            if (objEntr.GioNghi7 != null) { sql += "GioNghi7 = @GioNghi7,"; }
            if (objEntr.GioNghi8 != null) { sql += "GioNghi8 = @GioNghi8,"; }
            if (objEntr.GioNghi9 != null) { sql += "GioNghi9 = @GioNghi9,"; }
            if (objEntr.GioNghi10 != null) { sql += "GioNghi10 = @GioNghi10,"; }
            if (objEntr.GioNghi11 != null) { sql += "GioNghi11 = @GioNghi11,"; }
            if (objEntr.GioNghi12 != null) { sql += "GioNghi12 = @GioNghi12,"; }
            if (objEntr.GioNghi13 != null) { sql += "GioNghi13 = @GioNghi13,"; }
            if (objEntr.GioNghi14 != null) { sql += "GioNghi14 = @GioNghi14,"; }
            if (objEntr.GioNghi15 != null) { sql += "GioNghi15 = @GioNghi15,"; }
            if (objEntr.GioNghi16 != null) { sql += "GioNghi16 = @GioNghi16,"; }
            if (objEntr.GioNghi17 != null) { sql += "GioNghi17 = @GioNghi17,"; }
            if (objEntr.GioNghi18 != null) { sql += "GioNghi18 = @GioNghi18,"; }
            if (objEntr.GioNghi19 != null) { sql += "GioNghi19 = @GioNghi19,"; }
            if (objEntr.GioNghi20 != null) { sql += "GioNghi20 = @GioNghi20,"; }
            if (objEntr.SoGiaiDoan != null) { sql += "SoGiaiDoan = @SoGiaiDoan,"; }
            
/* Can not combine this 			if (objEntr.<%UpdateColumn(Name)%> != null) { sql += "<%UpdateColumn(FieldName)%> = @<%UpdateColumn(FieldName)%>,"; }*/

			//sql += " lastupdate = getdate()";
            sql = sql.Substring(0, sql.Length - 1);
            sql += @" WHERE  XuongId = @XuongId AND LineId = @LineId";

            openConnection();
            InitCommand(sql);

            AddParameter(OpGioInfo.Field.XuongId, objEntr.XuongId);
            AddParameter(OpGioInfo.Field.LineId, objEntr.LineId);

            if (objEntr.BatDau != null) AddParameter(OpGioInfo.Field.BatDau, objEntr.BatDau);         
            if (objEntr.GioLam1 != null) AddParameter(OpGioInfo.Field.GioLam1, objEntr.GioLam1);         
            if (objEntr.GioLam2 != null) AddParameter(OpGioInfo.Field.GioLam2, objEntr.GioLam2);         
            if (objEntr.GioLam3 != null) AddParameter(OpGioInfo.Field.GioLam3, objEntr.GioLam3);         
            if (objEntr.GioLam4 != null) AddParameter(OpGioInfo.Field.GioLam4, objEntr.GioLam4);         
            if (objEntr.GioLam5 != null) AddParameter(OpGioInfo.Field.GioLam5, objEntr.GioLam5);         
            if (objEntr.GioLam6 != null) AddParameter(OpGioInfo.Field.GioLam6, objEntr.GioLam6);         
            if (objEntr.GioLam7 != null) AddParameter(OpGioInfo.Field.GioLam7, objEntr.GioLam7);         
            if (objEntr.GioLam8 != null) AddParameter(OpGioInfo.Field.GioLam8, objEntr.GioLam8);         
            if (objEntr.GioLam9 != null) AddParameter(OpGioInfo.Field.GioLam9, objEntr.GioLam9);         
            if (objEntr.GioLam10 != null) AddParameter(OpGioInfo.Field.GioLam10, objEntr.GioLam10);         
            if (objEntr.GioLam11 != null) AddParameter(OpGioInfo.Field.GioLam11, objEntr.GioLam11);         
            if (objEntr.GioLam12 != null) AddParameter(OpGioInfo.Field.GioLam12, objEntr.GioLam12);         
            if (objEntr.GioLam13 != null) AddParameter(OpGioInfo.Field.GioLam13, objEntr.GioLam13);         
            if (objEntr.GioLam14 != null) AddParameter(OpGioInfo.Field.GioLam14, objEntr.GioLam14);         
            if (objEntr.GioLam15 != null) AddParameter(OpGioInfo.Field.GioLam15, objEntr.GioLam15);         
            if (objEntr.GioLam16 != null) AddParameter(OpGioInfo.Field.GioLam16, objEntr.GioLam16);         
            if (objEntr.GioLam17 != null) AddParameter(OpGioInfo.Field.GioLam17, objEntr.GioLam17);         
            if (objEntr.GioLam18 != null) AddParameter(OpGioInfo.Field.GioLam18, objEntr.GioLam18);         
            if (objEntr.GioLam19 != null) AddParameter(OpGioInfo.Field.GioLam19, objEntr.GioLam19);         
            if (objEntr.GioLam20 != null) AddParameter(OpGioInfo.Field.GioLam20, objEntr.GioLam20);         
            if (objEntr.GioNghi1 != null) AddParameter(OpGioInfo.Field.GioNghi1, objEntr.GioNghi1);         
            if (objEntr.GioNghi2 != null) AddParameter(OpGioInfo.Field.GioNghi2, objEntr.GioNghi2);         
            if (objEntr.GioNghi3 != null) AddParameter(OpGioInfo.Field.GioNghi3, objEntr.GioNghi3);         
            if (objEntr.GioNghi4 != null) AddParameter(OpGioInfo.Field.GioNghi4, objEntr.GioNghi4);         
            if (objEntr.GioNghi5 != null) AddParameter(OpGioInfo.Field.GioNghi5, objEntr.GioNghi5);         
            if (objEntr.GioNghi6 != null) AddParameter(OpGioInfo.Field.GioNghi6, objEntr.GioNghi6);         
            if (objEntr.GioNghi7 != null) AddParameter(OpGioInfo.Field.GioNghi7, objEntr.GioNghi7);         
            if (objEntr.GioNghi8 != null) AddParameter(OpGioInfo.Field.GioNghi8, objEntr.GioNghi8);         
            if (objEntr.GioNghi9 != null) AddParameter(OpGioInfo.Field.GioNghi9, objEntr.GioNghi9);         
            if (objEntr.GioNghi10 != null) AddParameter(OpGioInfo.Field.GioNghi10, objEntr.GioNghi10);         
            if (objEntr.GioNghi11 != null) AddParameter(OpGioInfo.Field.GioNghi11, objEntr.GioNghi11);         
            if (objEntr.GioNghi12 != null) AddParameter(OpGioInfo.Field.GioNghi12, objEntr.GioNghi12);         
            if (objEntr.GioNghi13 != null) AddParameter(OpGioInfo.Field.GioNghi13, objEntr.GioNghi13);         
            if (objEntr.GioNghi14 != null) AddParameter(OpGioInfo.Field.GioNghi14, objEntr.GioNghi14);         
            if (objEntr.GioNghi15 != null) AddParameter(OpGioInfo.Field.GioNghi15, objEntr.GioNghi15);         
            if (objEntr.GioNghi16 != null) AddParameter(OpGioInfo.Field.GioNghi16, objEntr.GioNghi16);         
            if (objEntr.GioNghi17 != null) AddParameter(OpGioInfo.Field.GioNghi17, objEntr.GioNghi17);         
            if (objEntr.GioNghi18 != null) AddParameter(OpGioInfo.Field.GioNghi18, objEntr.GioNghi18);         
            if (objEntr.GioNghi19 != null) AddParameter(OpGioInfo.Field.GioNghi19, objEntr.GioNghi19);         
            if (objEntr.GioNghi20 != null) AddParameter(OpGioInfo.Field.GioNghi20, objEntr.GioNghi20);
            if (objEntr.SoGiaiDoan != null) AddParameter(OpGioInfo.Field.SoGiaiDoan, objEntr.SoGiaiDoan);         
            
/* Can not combine this 			if (objEntr.<%UpdateColumn(Name)%> != null) AddParameter(OpGioInfo.Field.<%UpdateColumn(Name)%>, objEntr.<%UpdateColumn(Name)%>);          */
               
            string sErr = "";

            try { executeNonQuery(); }
            catch (Exception ex) { sErr = string.Format("{0} sql='{1}'", ex.Message, sql); }
            finally { closeConnection(); }

            return sErr;
        }

        public string Delete(String XuongId, Int32 LineId) {
            string sql = @"DELETE OpGio
            WHERE 	 XuongId = @XuongId AND LineId = @LineId";

            openConnection();
            InitCommand(sql);

            AddParameter(OpGioInfo.Field.XuongId, XuongId);
            AddParameter(OpGioInfo.Field.LineId, LineId);
              
            string sErr = "";
            try { executeNonQuery(); }
            catch (Exception ex) { sErr = string.Format("{0} sql='{1}'", ex.Message, sql); }
            finally { closeConnection(); }

            return sErr;
        }   	
        
        public Boolean IsExist(String XuongId, Int32? LineId, ref string sErr) {
            string sql = @"SELECT TOP (1) XuongId, LineId
            FROM  OpGio
            WHERE 	 XuongId = @XuongId AND LineId = @LineId";
			
            InitConnect();
            InitCommand(sql);
            AddParameter(OpGioInfo.Field.XuongId, XuongId);              
            AddParameter(OpGioInfo.Field.LineId, LineId);              
            
            DataTable list = new DataTable();
            try { list = executeSelect(); }
            catch (Exception ex) { sErr = ex.Message + " sql='" + sql + "'"; }

			return list.Rows.Count == 1;
        }		
		
		#endregion Method
     
    }
}
